package com.neusoft.databus.policy.converter;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.databus.policy.dto.PolicyDTO;
import com.neusoft.databus.policy.model.Policy;
import com.neusoft.databus.policy.model.PolicyRefResource;
import com.neusoft.databus.policy.repository.PolicyRepository;

/**
 * 策略Converter
 *
 * @author naxiang
 */
@Component
public class PolicyConverter extends AbstractConverter<Policy, PolicyDTO> {
    @Autowired
    private ExpressionConverter expressionConverter;

    @Autowired
    private PolicyRepository policyRepository;

    @Autowired
    private BaseIdGenerator policyCodeGenerator;
    @Autowired
    private PolicyRefResourceConverter policyRefResourceConverter;
    @Autowired
    private PolicyRefLinkageConverter policyRefLinkageConverter;

    @Override
    public Policy toModel(PolicyDTO dto) {
        Policy model;
        if (dto.isNew()) {
            model = new Policy();
            if (StringUtils.isNotBlank(dto.getCode())) {
                model.setCode(dto.getCode());
            } else {
                model.setCode(this.policyCodeGenerator.generateCode());
            }
        } else {
            model = this.policyRepository.getOne(dto.getId());
        }
        model.setName(dto.getName());
        model.setDisabled(dto.isDisabled());
        model.setExtendProperty(dto.getExtendProperty());
        model.setDescription(dto.getDescription());
        model.setCriterion(dto.getCriterion());
        model.setPolicyType(dto.getPolicyType());
        model.setContentTemplate(dto.getContentTemplate());
        if (dto.getExpressions().size() > 0) {
            model.setExpressions(this.expressionConverter.toListModel(dto.getExpressions()));
        }
        if (StringUtils.isNoneBlank(dto.getResourceCode())) {
            final List<PolicyRefResource> policyRefResources = Lists.newArrayList();
            policyRefResources.add(this.policyRefResourceConverter.toModel(dto.getResourceCode()));
            model.setPolicyRefResources(policyRefResources);
        } else {
            if (dto.getPolicyRefResources().size() > 0) {
                model.setPolicyRefResources(this.policyRefResourceConverter.toListModel(dto.getPolicyRefResources()));
            }
        }
        model.setCreateTime(dto.getCreateTime());
        model.setUpdateTime(dto.getUpdateTime());
        return model;
    }

    @Override
    public PolicyDTO toDTO(Policy model, boolean forListView) {
        final PolicyDTO dto = new PolicyDTO();
        dto.setCode(model.getCode());
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setDisabled(model.isDisabled());
        dto.setExtendProperty(model.getExtendProperty());
        dto.setDescription(model.getDescription());
        dto.setCriterion(model.getCriterion());
        dto.setPolicyType(model.getPolicyType());
        dto.setContentTemplate(model.getContentTemplate());
        // dto.setEventPriority(model.getEventPriority());
        dto.setExpressions(this.expressionConverter.toListDTO(model.getExpressions()));
        dto.setPolicyRefResources(this.policyRefResourceConverter.toListDTO(model.getPolicyRefResources()));
        dto.setPolicyRefLinkages(this.policyRefLinkageConverter.toListDTO(model.getPolicyRefLinkages()));
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        return dto;
    }

}
